package com.zhoug.player.vlcplayer;

import android.content.Context;
import android.text.TextUtils;
import android.view.TextureView;

import com.zhoug.player.core.BaseZPlayer;
import com.zhoug.player.core.PlayError;
import com.zhoug.player.core.ZPlayer;
import com.zhoug.player.core.utils.LogUtils;

import androidx.annotation.Nullable;

/**
 * @Author: zhoug
 * @Date: 2024-06-22
 * @Description:
 */
public class VLCPlayerHelper extends BaseZPlayer {
    private static final String TAG = ">>>VLCPlayerHelper";
    private VLCPlayer6 mPlayer;

    public VLCPlayerHelper() {
    }

    public VLCPlayerHelper(Context context) {
        mContext = context;
    }

    @Override
    public void setMute(boolean mute) {
        super.setMute(mute);
        _setMute();
    }

    @Override
    public void setVideoTextureView(@Nullable TextureView textureView) {
        super.setVideoTextureView(textureView);
        _setTextureView();
    }

    @Override
    public void startPlay() {
        if (TextUtils.isEmpty(mPlayUrl)) {
            callError(PlayError.URL_IS_EMPTY, "playUrl为空");
            return;
        }
        if (mPlayer == null) {
            initPlayer();
            if (mPlayer == null) {
                callError(PlayError.PLAYER_INIT_FAILURE, "初始化播放器失败");
                return;
            }
        }
        mPlayer.setDataSource(mPlayUrl);
        mPlayer.play();
        LogUtils.d(TAG, "startPlay:开始播放:" + mPlayUrl);
        callStatusChange(ZPlayer.STATE_START);
    }

    @Override
    public void stopPlay() {
        if (mPlayer != null) {
            mPlayer.stop();
            LogUtils.d(TAG, "stopPlay:停止播放");
        }
    }

    @Override
    public void pausePlay() {
        if (mPlayer != null) {
            mPlayer.pause();
            LogUtils.d(TAG, "pausePlay:暂停播放");

        }
    }

    @Override
    public void resumePlay() {
        if (mPlayer != null) {
            mPlayer.play();
            LogUtils.d(TAG, "resumePlay:恢复播放");
        }
    }

    @Override
    public void seekTo(long positionMs) {

    }

    @Override
    public void startRecordVideo(String path) {
        if(mPlayer!=null){
            boolean b = mPlayer.startRecording(path);
            LogUtils.d(TAG, "startRecordVideo:"+b);
        }
    }

    @Override
    public void stopRecordVideo() {
        if(mPlayer!=null){
            boolean b = mPlayer.stopRecording();
            LogUtils.d(TAG, "stopRecordVideo:"+b);
        }
    }

    @Override
    public void startVoiceTalk() {

    }

    @Override
    public void stopVoiceTalk() {

    }

    @Override
    public boolean isTalking() {
        return false;
    }

    @Override
    public void release() {
        super.release();
        if(mPlayer!=null){
            mPlayer.release();
            mPlayer=null;
        }
    }

    private void initPlayer() {
        if (mPlayer == null) {
            synchronized (this) {
                if (mPlayer == null) {
                    LogUtils.d(TAG, "initPlayer:初始化播放器");
                    try {
                        mPlayer = new VLCPlayer6(mContext);
                        _setTextureView();
                        mPlayer.setCallback(vlcPlayerCallback);
                        _setMute();
                    } catch (Exception e) {
                        e.printStackTrace();
                        callError(PlayError.PLAYER_INIT_FAILURE, "初始化播放器失败:" + e.getMessage());
                    }
                }
            }
        }
    }

    private VLCPlayer6.VLCPlayerCallback vlcPlayerCallback = new VLCPlayer6.VLCPlayerCallback() {
        @Override
        public void onStart() {
            callStatusChange(ZPlayer.STATE_START);
        }

        @Override
        public void onBuffering(float bufferPercent) {
//            LogUtils.d(TAG, "正在缓冲数据:"+bufferPercent);
//            if(bufferPercent>=100){
//                callStatusChange(ZPlayer.STATE_PLAYING);
//            }
        }



        @Override
        public void onPlaying() {
            callStatusChange(ZPlayer.STATE_PLAYING);
        }

        @Override
        public void onPaused() {
            callStatusChange(ZPlayer.STATE_PAUSE);
        }

        @Override
        public void onStopped() {
            callStatusChange(ZPlayer.STATE_IDLE);
        }

        @Override
        public void onEndReached() {
            callStatusChange(ZPlayer.STATE_ENDED);
        }

        @Override
        public void onError() {
            callError(PlayError.UNKNOWN_ERROR, "播放错误");

        }

        @Override
        public void onTotalTime(long totalTime) {
            if (mPlayProgressListener != null) {
                mPlayProgressListener.onDurationChange(totalTime);
            }
        }

        @Override
        public void onTimeChanged(long currentTime) {
            if (mPlayProgressListener != null) {
                mPlayProgressListener.onPositionChange(currentTime);
            }
        }

        @Override
        public void onPositionChanged(float position) {

        }
    };

    private void _setMute() {
        if (mPlayer != null) {
            mPlayer.setMute(mMute);
        }
    }

    private void _setTextureView() {
        if (mPlayer != null) {
            mPlayer.setVideoSurface(mTextureView);
        }
    }


}
